// pages/msg_yield_add_details/msg_yield_add_details.js
let app = getApp();


Page({

    /**
     * 页面的初始数据
     */
    data: {
        valuezhi: '输入长度显示',
        listweo: false,
        imgList: [],
        staff: [{
            'LUSERNAME':app.globalData.lusercode
        }],
        chejian: [],
        LLONG: 0,
        LLWEIGHT: 0,
        LPHOTO: ''
    },
    my_img(e) {
        wx.previewImage({
            current: this.data.imgList[0], // 当前显示图片的http链接
            urls: this.data.imgList // 需要预览的图片http链接列表
        })
    },
    bindinputip(e) {
        this.setData({
            LLONG: e.detail.value
        })
        if (this.data.listweo == false) {

        } else {
            this.data.LLWEIGHT = parseInt(this.data.LLONG) * parseInt(this.data.chejian[0].LCONVERSION)
            this.setData({
                LLWEIGHT: this.data.LLWEIGHT
            })
        }
    },
    bindinputip1(e) {
        this.setData({
            LLWEIGHT: e.detail.value
        })
    },
    lramecm() {
        var that = this
        wx.scanCode({
            onlyFromCamera: true,
            scanType: 'qrCode',
            success(res) {
                console.log('扫码成功', res)
                let result = res.result
                wx.request({
                    url: 'https://' + app.globalData.url + '/machineInfo/findMachineInfoByLID',
                    data: {
                        LID: result
                    },
                    method: 'GET',
                    dataType: 'json',
                    responseType: 'text',
                    header: {
                        'content-type': 'application/json;charset=utf-8'
                    },
                    // 访问成功
                    success: function (res) {
                        if (res.data.success == true) {
                            that.setData({
                                chejian: res.data.obj
                            })
                            if (that.data.chejian[0].LPRCNAME == '梳棉' || that.data.chejian[0].LPRCNAME == '并条' || that.data.chejian[0].LPRCNAME == '粗纱' || that.data.chejian[0].LPRCNAME == '细纱') {
                                that.setData({
                                    listweo: true,
                                })
                                console.log(that.data.chejian[0].LPRCNAME)
                            } else {
                                that.setData({
                                    listweo: false,
                                    valuezhi: '手动输入'
                                })
                            }
                        } else {
                            wx.showModal({
                                title: '提示',
                                content: '获取数据失败',
                                showCancel: false,
                                confirmText: '确定',
                                success(res) {
                                    if (res.confirm) {}
                                }
                            })
                        }
                    },
                    fail: function (res) {
                        wx.showModal({
                            title: '提示',
                            content: '访问服务器失败！',
                            showCancel: false,
                            confirmText: '确定',
                            success(res) {
                                if (res.confirm) {
                                    console.log('用户点击确定')

                                }
                            }
                        })
                    },
                })
            },
            fail(res) {

                wx.showModal({
                    title: '提示',
                    content: '扫描二维码失败!',
                    showCancel: false,
                    confirmText: '确定',
                    success(res) {
                        if (res.confirm) {

                        }
                    }
                })
            }
        })
    },
    //扫码按钮
    bindsm: function (e) {
        var that = this
        if (that.data.staff.length > 4) {
            wx.showModal({
                title: '提示',
                content: '作业员最多五位',
                showCancel: false,
                confirmText: '确定',
                success(res) {
                    if (res.confirm) {

                    }
                }
            })
        } else {
            wx.scanCode({
                onlyFromCamera: true,
                scanType: 'qrCode',
                success(res) {
                    console.log('扫码成功', res)
                    let result = res.result
                    wx.request({
                        url: 'https://' + app.globalData.url + '/staff/findStaffByLID',
                        data: {
                            LID: result
                        },
                        method: 'GET',
                        dataType: 'json',
                        responseType: 'text',
                        header: {
                            'content-type': 'application/json;charset=utf-8'
                        },
                        // 访问成功
                        success: function (res) {
                            if (res.data.success == true) {
                                if (that.data.staff[0].LUSERNAME == undefined) {
                                    that.setData({
                                        staff: []
                                    })
                                    that.data.staff.push(res.data.obj[0])
                                    that.setData({
                                        staff: that.data.staff
                                    })

                                } else {
                                    that.data.staff.push(res.data.obj[0])
                                    that.setData({
                                        staff: that.data.staff
                                    })
                                }


                            } else {
                                wx.showModal({
                                    title: '提示',
                                    content: '获取数据失败',
                                    showCancel: false,
                                    confirmText: '确定',
                                    success(res) {
                                        if (res.confirm) {

                                        }
                                    }
                                })
                            }
                        },
                        fail: function (res) {
                            wx.showModal({
                                title: '提示',
                                content: '访问服务器失败！',
                                showCancel: false,
                                confirmText: '确定',
                                success(res) {
                                    if (res.confirm) {
                                        console.log('用户点击确定')

                                    }
                                }
                            })
                        },
                    })
                },
                fail(res) {

                    wx.showModal({
                        title: '提示',
                        content: '扫描二维码失败!',
                        showCancel: false,
                        confirmText: '确定',
                        success(res) {
                            if (res.confirm) {

                            }
                        }
                    })
                }
            })
        }



    },
    shanchu(e) {
        let list = this.data.staff
        console.log(this.data.staff.length)
        if (this.data.staff.length == 1) {
            list.splice(e.currentTarget.dataset.index, 1)
            this.setData({
                staff: list,
            })
            this.setData({
                staff: [{}]
            })
            wx.showToast({
                title: '成功',
                icon: 'success',
                duration: 2000
            })
        } else {
            list.splice(e.currentTarget.dataset.index, 1)
            this.setData({
                staff: list,
            })
            wx.showToast({
                title: '成功',
                icon: 'success',
                duration: 2000
            })
        }
        console.log(e.currentTarget.dataset.index)

    },
    // ListTouch触摸开始
    ListTouchStart(e) {
        this.setData({
            ListTouchStart: e.touches[0].pageX
        })
    },
    // ListTouch计算方向
    ListTouchMove(e) {
        this.setData({
            ListTouchDirection: e.touches[0].pageX - this.data.ListTouchStart > 0 ? 'right' : 'left'
        })
    },
    // ListTouch计算滚动
    ListTouchEnd(e) {
        console.log(e.currentTarget.dataset.target)
        if (this.data.ListTouchDirection == 'left') {
            this.setData({
                modalName: e.currentTarget.dataset.target
            })
        } else {
            this.setData({
                modalName: null
            })
        }
        this.setData({
            ListTouchDirection: null
        })
    },
    ViewImage(e) {
        wx.previewImage({
            urls: this.data.imgList,
            current: e.currentTarget.dataset.url
        });
    },
    ChooseImage() {
        let that = this
        wx.chooseImage({
            count: 1, //默认9
            sizeType: 'compressed', //可以指定是原图还是压缩图，默认二者都有
            sourceType: ['camera'], //从相册选择
            success: (res) => {
                if (this.data.imgList.length != 0) {
                    this.setData({
                        imgList: this.data.imgList.concat(res.tempFilePaths)
                    })
                } else {
                    this.setData({
                        imgList: res.tempFilePaths
                    })
                    console.log(this.data.imgList)
                    wx.uploadFile({
                        url: 'https://' + app.globalData.url + '/fileupload',
                        filePath: that.data.imgList[0],
                        name: 'file',
                        header: {
                            'content-type': 'multipart/form-data'
                        },
                        success(res) {

                        },
                        fail: function (res) {
                            console.log('访问服务器出错！！！', res.errMsg);
                        },
                    })
                }
            }
        });
    },
    DelImg(e) {
        wx.showModal({
            title: '你好！',
            content: '确定要删除这张图片吗？',
            cancelText: '再看看',
            confirmText: '再见',
            success: res => {
                if (res.confirm) {
                    this.data.imgList.splice(e.currentTarget.dataset.index, 1);
                    this.setData({
                        imgList: this.data.imgList
                    })
                }
            }
        })
    },
    update1() {
        let that = this
        if (that.data.chejian == '') {
            wx.showModal({
                title: '提示',
                content: '请扫描车间!',
                showCancel: false,
                confirmText: '确定',
                success(res) {
                    if (res.confirm) {

                    }
                }
            })
        } else if (that.data.staff[0].LUSERNAME == undefined) {
            wx.showModal({
                title: '提示',
                content: '请扫描作业员!',
                showCancel: false,
                confirmText: '确定',
                success(res) {
                    if (res.confirm) {

                    }
                }
            })
        } else if (that.data.LLWEIGHT == 0) {
            wx.showModal({
                title: '提示',
                content: '请输入重量!',
                showCancel: false,
                confirmText: '确定',
                success(res) {
                    if (res.confirm) {

                    }
                }
            })
        } else if (that.data.imgList == '') {
            wx.showModal({
                title: '提示',
                content: '请上传图片!',
                showCancel: false,
                confirmText: '确定',
                success(res) {
                    if (res.confirm) {

                    }
                }
            })
        } else {
            for(var i = 0; i < that.data.staff.length;i ++) {
                if(that.data.staff.length < 2) {
                    that.data.staff.push({},{},{},{})
                } else if(that.data.staff.length < 3) {
                    that.data.staff.push({},{},{})
                } else if(that.data.staff.length < 4) {
                    that.data.staff.push({},{})
                } else if(that.data.staff.length < 5) {
                    that.data.staff.push({})
                } 
            }
            app.globalData.my_table = {
                "LFARM": that.data.chejian[0].LFARMNAME, //车间
                'LPRCCODE': that.data.chejian[0].LPRCNAME, //工序
                'LMACHINENO': that.data.chejian[0].LMACHINENAME, // 机台
                'LUSERCODE': that.data.staff, // 作业员
                'LLWEIGHT': that.data.LLWEIGHT, //产量
                'LLONG': that.data.LLONG,
                'LPHOTO': that.data.imgList[0].slice(9)
            }
            app.globalData.newtable.push(app.globalData.my_table)
            wx.navigateBack({
                delta: 1
            })
            wx.navigateTo({
                url: '../msg_yield_add_details/msg_yield_add_details?list=' + '1'
            })
            wx.showToast({
                title: '成功',
                icon: 'success',
                duration: 2000
            })
        }
    },
    update2() {
        let that = this
        if (that.data.chejian == '') {
            wx.showModal({
                title: '提示',
                content: '请扫描车间!',
                showCancel: false,
                confirmText: '确定',
                success(res) {
                    if (res.confirm) {

                    }
                }
            })
        } else if (that.data.staff[0].LUSERNAME == undefined) {
            wx.showModal({
                title: '提示',
                content: '请扫描作业员!',
                showCancel: false,
                confirmText: '确定',
                success(res) {
                    if (res.confirm) {

                    }
                }
            })
        } else if (that.data.LLWEIGHT == '') {
            wx.showModal({
                title: '提示',
                content: '请输入重量!',
                showCancel: false,
                confirmText: '确定',
                success(res) {
                    if (res.confirm) {

                    }
                }
            })
        } else  if (that.data.imgList == '') {
            wx.showModal({
                title: '提示',
                content: '请上传图片!',
                showCancel: false,
                confirmText: '确定',
                success(res) {
                    if (res.confirm) {

                    }
                }
            })
        } else {
            for(var i = 0; i < that.data.staff.length;i ++) {
                if(that.data.staff.length < 2) {
                    that.data.staff.push({},{},{},{})
                } else if(that.data.staff.length < 3) {
                    that.data.staff.push({},{},{})
                } else if(that.data.staff.length < 4) {
                    that.data.staff.push({},{})
                } else if(that.data.staff.length < 5) {
                    that.data.staff.push({})
                } 
            }
            app.globalData.my_table = {
                "LFARM": that.data.chejian[0].LFARMNAME, //车间
                'LPRCCODE': that.data.chejian[0].LPRCNAME, //工序
                'LMACHINENO': that.data.chejian[0].LMACHINENAME, // 机台
                'LUSERCODE': that.data.staff, // 作业员
                'LLWEIGHT': that.data.LLWEIGHT, //产量
                'LLONG': that.data.LLONG,
                'LPHOTO': that.data.imgList[0].slice(9)
            }
            app.globalData.newtable.push({
                "LFARM": that.data.chejian[0].LFARMNAME, //车间
                'LPRCCODE': that.data.chejian[0].LPRCNAME, //工序
                'LMACHINENO': that.data.chejian[0].LMACHINENAME, // 机台
                'LUSERCODE': that.data.staff, // 作业员
                'LLWEIGHT': that.data.LLWEIGHT, //产量
                'LLONG': that.data.LLONG,
                'LPHOTO': that.data.imgList[0]
            })
            wx.navigateBack({
                delta: 1
            })
            wx.showToast({
                title: '成功',
                icon: 'success',
                duration: 2000
            })
        }

    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let that = this
        console.log(this.data.staff)
        this.data.staff[0].LUSERNAME = app.globalData.lusername
        this.data.staff[0].LUSERCODE = app.globalData.lusercode
        console.log(app.globalData.lusercode)
        this.setData({
            list: options.list,
            staff:this.data.staff
        })
        if (options.list == 2) {
            wx.request({
                url: 'https://' + app.globalData.url + '/SaleOrder/findSaleOrderDts',
                data: {
                    LID: app.globalData.notice_lid,
                    LSEQ: app.globalData.notice_lseq,
                    LMAKEOPID: app.globalData.notice_lmakeopid
                },
                method: 'GET',
                dataType: 'json',
                responseType: 'text',
                header: {
                    'content-type': 'application/json;charset=utf-8'
                },
                success: function (res) {
                    if (res.data.success == true) {
                        that.setData({
                            table: res.data.obj
                        })
                        that.data.table[0].LPHOTO = "https://www.hanliangroup.com:8443/image/UserImage/" + that.data.table[0].LPHOTO
                        that.data.imgList.push(that.data.table[0].LPHOTO)

                        that.setData({
                            imgList: that.data.imgList
                        })
                        console.log(that.data.imgList)
                        for (var i = 0; i < that.data.table.length; i++) {
                            that.data.staff.push(that.data.table[i])
                            that.setData({
                                staff: that.data.staff
                            })
                        }
                        console.log(that.data.staff)
                    } else {
                        wx.showModal({
                            title: '提示',
                            content: '获取数据失败',
                            showCancel: false,
                            confirmText: '确定',
                            success(res) {
                                if (res.confirm) {}
                            }
                        })
                    }
                },
                fail: function (res) {
                    that.setData({
                        loadModal: false
                    })
                    wx.showModal({
                        title: '提示',
                        content: '访问服务器出错',
                        showCancel: false,
                        confirmText: '确定',
                        success(res) {
                            if (res.confirm) {}
                        }
                    })
                }
            })

        }
        if (options.list == 3) {
            console.log(options.index)
            console.log(app.globalData.newtable)
            for (var i = 0; i < app.globalData.newtable.length; i++) {
                if (options.index == i) {
                    that.setData({
                        table: app.globalData.newtable[i]
                    })
                    console.log(that.data.table.LUSERCODE)
                }
            }
            that.data.imgList.push(that.data.table.LPHOTO)

            that.setData({
                imgList: that.data.imgList
            })
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})